package net.aib.moneytracker;

import net.aib.moneytracker.db.DBHelper;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class CategoriesList extends ListActivity {
	private  final static String TAG = "CategoriesList";
	
	private MoneyTrackerController mMTController;
	private DBHelper mDBHelper;
	private Cursor mCursor;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

        setContentView(R.layout.categorieslist);

        mMTController = new MoneyTrackerController(this);
        mCursor = mMTController.getGroups(); 
        startManagingCursor(mCursor);

        ListAdapter adapter = new SimpleCursorAdapter(
                this,
                R.layout.categorieslistrow,
                mCursor, 
                new String[] {mDBHelper.CATEGORY_KEY_NAME},  
                new int[] {R.id.group});

        setListAdapter(adapter);
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		final CharSequence[] items = {getText(R.string.edit), getText(R.string.delete)};
		final long itemid = id;
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		
		builder.setTitle(R.string.pick_a_task);
		builder.setItems(items, new OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				if(items[which].toString().compareTo(getText(R.string.edit).toString()) == 0)
				{
					Log.d(TAG, "id = " + itemid);
					editCategory(itemid,mMTController.getCategory(itemid));
					dialog.dismiss();
				}
				else
				{
					deleteCategory(itemid);
					dialog.dismiss();
				}
			}
		});
		builder.show();
	}
	
	public void deleteCategory(long id)
	{
		final long itemId = id;
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		
		builder.setTitle(R.string.are_you_sure);
		builder.setPositiveButton(R.string.yes, new OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				mMTController.deleteCategory(itemId);
				mCursor.requery();
				dialog.dismiss();
			}
		});
		builder.setNegativeButton(R.string.no, new OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				
			}
		});
		builder.show();
	}
	
	public void editCategory(long id, String name){
    	AlertDialog.Builder builder;
    	AlertDialog categoryDialog;
    	
    	LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
    	View layout = inflater.inflate(R.layout.editcategory, null);
    	builder = new AlertDialog.Builder(this);
    	final EditText eGroup = (EditText) layout.findViewById(R.id.group_name);
    	final long catid = id;
    	
    	eGroup.setText(name);
    	
    	builder.setView(layout)
    		   .setTitle(R.string.edit_group)
    		   .setPositiveButton(R.string.change, new OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					if(eGroup.getText().length() > 0){
						mMTController.editCategory(catid, eGroup.getText().toString());
						mCursor.requery();
						dialog.dismiss();
					}
					else{
						Toast.makeText(getApplicationContext(), "Vantar texta", Toast.LENGTH_LONG).show();
					}
				} 
				})
			   .setNegativeButton(R.string.cancel, new OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});
    	categoryDialog = builder.create();
    	categoryDialog.show();
    }
}
